<?PHP	//header
include_once('config/config.php');
include_once('php/header.php');
require_once('php/functions.php');

// connect to database
$conn = mysql_connect($cfg["connection"]["host"], $cfg["connection"]["userName"], $cfg["connection"]["password"]) 
	or die ('Error Cannot Connect to MySQL');
@mysql_select_db($cfg["connection"]["database"]) or die( "Unable to select database");
?>        
<?PHP
//	if (session_status() == PHP_SESSION_NONE) {
		session_start();
//	}
	if (isset($_GET['bar']) and sanityCheck($_GET['bar'], string, 8) and barcodeLengthCheck($_GET['bar'], 8)) {	
		$temp = db_escape_string($_GET['bar']);
		$sql = "SELECT COUNT(*) FROM item WHERE itemId = ".$temp.";";
		$res = mysql_query($sql);
		$res = mysql_fetch_array($res);
		if ($res[0]) {
			echo("<div class=\"valid_box\">Analytics for Item with Barcode ".$temp." is now Availabe</div>");
			$_SESSION['barcode_visualize'] = db_escape_string($_GET['bar']);	
		}
		else {
			echo("<div class=\"error_box\">Item with Barcode ".$temp." is no longer Availabe</div>");
			$_SESSION['barcode_visualize'] = 18417740;
		}
	}
	else {
	//$sql = "DELETE FROM $tbl_name WHERE $column_name = $column_targetVal";
		//		$result = mysql_query($sql);
		echo("<div class=\"warning_box\">Fill in a proper barcode and Click Submit</div>");
		$_SESSION['barcode_visualize'] = 18417740;
	}
	$sql = "SELECT * FROM item WHERE itemId = ".$_SESSION['barcode_visualize'].";";
	$result = mysql_query($sql) or die(mysql_error());
?>
	<h2>Enter a Barcode</h2>
         <div class="form">
         <form action="stockTrend.php" method="GET" id="SearchForm" class="niceform">
                <fieldset>
                    <dl>
                        <dt><label for="password">Barcode:</label></dt>
                        <dd><input type="text" name="bar" size="54" <?php if (isset($_SESSION['barcode_visualize'])) echo "placeholder=".$_SESSION['barcode_visualize'];
																			else echo "placeholder=18417740"; 
																			?>></dd>
                    </dl>
                    
                     <dl class="submit">
                    <input type="submit" name="submit" id="submit" value="Submit" />
                     </dl>  
                </fieldset>       
         </form>
         </div>  
	<h2><a name="prod">Product Information</a></h2> 
    <div>                
		<table id="rounded-corner" summary="Inventory Database">
			<thead>
				<tr>
					<th scope="col" class="rounded-company"></th>
					<th scope="col" class="rounded">Barcode</th>
					<th scope="col" class="rounded">Product Name</th>
					<th scope="col" class="rounded">Manufacturer</th>
					<th scope="col" class="rounded">Category</th>
					<th scope="col" class="rounded">Current Stock</th>
					<th scope="col" class="rounded">Current Price</th>
				</tr>
			</thead>

			<tbody>
			<?PHP	//output from database to main window's box
			echo '<div class="divOuter">';		//parent div ----- just to format multiple children divs inline
			
					while($row = mysql_fetch_array($result)){
						echo "<tr>";
							echo "<td><input type=\"checkbox\" name=\"checkbox_Inventory[]\" value='".$row[$cfg["tbl_inventory"]["barcode"]]."'/></td>";
							echo "<td>".$row[$cfg["tbl_inventory"]["barcode"]]."</td>";
							echo "<td>".$row[$cfg["tbl_inventory"]["product_name"]]."</td>";
							echo "<td>".$row[$cfg["tbl_inventory"]['manufacturer']]."</td>";
							echo "<td>".$row[$cfg["tbl_inventory"]['category']]."</td>";
							echo "<td>".$row[$cfg["tbl_inventory"]['local_stock']]."</td>";
							echo "<td>".$row[$cfg["tbl_inventory"]['price']]."</td>";
						echo "</tr>";
					}
			echo '</div>';
			?>
			</tbody>
		</table>
	</div>
    <h2 id="pageName">Sales Summary and Price Estimator</h2> 
	<div>
		<?php
			
			// The name of the graph script file (change as needed!)
			$_graphfilename = 'salesVisual.php';
			 
			// This is the filename of this HTML file
			global $_wrapperfilename;
			$_wrapperfilename = basename (__FILE__);
			 
			// Create a random mapname used to connect the image map with the image
			$_mapname = '__mapname'.rand(0,1000000).'__';
			 
			// This is the actual graph script
			require_once ($_graphfilename);
			 
			// Get hold of the image map to include it in the HTML page
			$imgmap = $graph->GetHTMLImageMap($_mapname);
			echo $imgmap;

			// We now create the <img> tag
			$imgtag = $graph->GetCSIMImgHTML($_mapname,$_graphfilename);
			echo $imgtag;
		?>
	</div>
	<h2 id="pageName">Estimated Price-Stabilization: $<?PHP if (isset($_SESSION['price_estimator'])) echo round($_SESSION['price_estimator'],2); ?> </h2> 
	
<div class="pagination">
<?PHP	//footer
include('php/footer.php');
?>

